package com.example.user.customviewdemo.pulltorefresh;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;

import com.example.user.customviewdemo.R;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

public class PullToRefreshListViewActivity extends AppCompatActivity {

    @Bind(R.id.pull_to_refresh_list_view)
    PullToRefreshListView pullToRefreshListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pull_to_refresh_list_view);
        ButterKnife.bind(this);

        final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,getData());
        pullToRefreshListView.setAdapter(adapter);

       pullToRefreshListView.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
           @Override
           public void onRefresh() {
               new AsyncTask<Void,Void,Void>(){
                   @Override
                   protected Void doInBackground(Void... params) {
                       try {
                           Thread.sleep(3000);
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                       return null;
                   }

                   @Override
                   protected void onPostExecute(Void aVoid) {
                       super.onPostExecute(aVoid);
                       adapter.notifyDataSetChanged();
                   }
               }.execute();
           }
       });
    }

    private ArrayList<String> getData(){
        ArrayList<String> data = new ArrayList<>();
        for (int i = 0;i<20;i++){
            data.add("第"+i+"项");
        }
        return data;
    }
}
